$(function () {

    $("#role_table").datagrid({onSelect: function (rowIndex, rowData) {

        $("#role_catalog").tree({
            url: "catalog/list/role?roleId=" + rowData.roleId
        });
//        $.ajax({
//            type: 'POST',
//            url: "catalog/list/role?roleId=" + rowData.roleId,
//            dataType: "json",
//            cache: false,
//            success: function (data) {
//                $("#role_catalog").oneTime('0.2s', function () {
//                    $("#role_catalog").tree("loadData", data);
//                });
//            }
//        });

        $("#role_member").datagrid({
            url: "role/"+ rowData.roleId+"/list/member"
        });

//        $.ajax({
//            type: 'POST',
//            url: "role/"+ rowData.roleId+"/list/member" ,
//            dataType: "json",
//            cache: false,
//            success: function (data) {
//                $("#role_member").oneTime('0.2s', function () {
//                    $("#role_member").datagrid("loadData", data);
//                });
//            }
//        });

    }});
})

function add_role_member_formatter(value, row, index) {
    if (row) {
        $("#role_table").oneTime('0.2s', function () {
            $("#add_role_" + row.roleId).linkbutton({
                iconCls: "icon-add",
                plain: true
            }).click(function () {
                    $("#add_member_dlg").dialog("open");
                    $("#member_combo").combogrid("clear");
                    $("#member_combo").combogrid({
                        panelWidth: 300,
                        idField: 'id',
                        textField: 'name',
                        url: "role/list/notIn/" + row.roleId + "/member",
                        columns: [
                            [
                                {field: 'name', title: '姓名', align: 'center', width: 80},
                                {field: 'sex', title: '性别', align: 'center', width: 60,
                                    formatter: function (value, row, index) {
                                        return sex[value];
                                    }
                                },
                                {field: 'identityCardNumber', title: '身份证号码', align: 'center', width: 60},
                                {field: 'birthday', title: '生日', align: 'center', width: 60},
                                {field: 'nation', title: '民族', align: 'center', width: 60, formatter: function (value, row, index) {
                                    return nation[value-1];
                                }},
                                {field: 'subject', title: '科目', align: 'center', width: 60},
                                {field: 'grade', title: '任课班级', align: 'center', width: 60}
                            ]
                        ],
                        fitColumns: true
                    });
                    $("#add_member_submit").click(function () {
                        var roleId=$("#role_table").datagrid("getSelected").roleId;
                        var memberId = $('#member_combo').combogrid('getValue');
                        if (memberId) {
                            $.ajax({
                                type: 'POST',
                                url: "role/" + roleId + "/add/member/" + memberId,
                                dataType: "json",
                                cache: false,
                                success: function (data) {
                                    $("#role_member").oneTime('0.2s', function () {
                                        $("#role_member").datagrid("appendRow", data);
                                    });
                                }
                            });
                        }
                        $('#member_combo').combogrid("clear");
                        $("#add_member_dlg").dialog("close");
                    });
                });
        });
        return "<a id='add_role_" + row.roleId + "' href='#'>添加成员</a>";
    }
};

function delete_role_member_formatter(value, row, index) {
    if (row) {
        $("#role_member").oneTime('0.2s', function () {
            $("#delete_role_" + row.memberId).linkbutton({
                iconCls: "icon-remove",
                plain: true
            }).click(function () {
                    var roleId = $("#role_table").datagrid("getSelected").roleId;
                    var memberId = row.memberId;
                    $.ajax({
                        type: 'POST',
                        url: "role/" + roleId + "/delete/member/" + memberId,
                        dataType: "json",
                        cache: false,
                        success: function (data) {
                            $("#role_member").oneTime('0.2s', function () {
                                if (data) {
                                    $("#role_member").datagrid("deleteRow", index);
                                }
                            });
                        }
                    });
                });
        });
        return "<a id='delete_role_" + row.memberId + "' href='#'>删除成员</a>";
    }
};

function user_type_formatter(value, row, index) {
    if (row) {
        if (value == 1) {
            return "学生";
        } else {
            return "老师";
        }
    }
    return value;
};